﻿using System.Numerics;

namespace NodeBasedSkillEditor
{
    public class Value_Vector2 : AValue
    {
        public override EValueType ValueType { get { return EValueType.Vector2; } }

        public Vector2 Value;

        public Value_Vector2(Vector2 v)
        {
            Value = v;
        }

        public Value_Vector2(Vector2Port portData)
        {
            Value = portData.Value;
        }

        public override AValue Clone()
        {
            return new Value_Vector2(Value);
        }

        public override (bool, Vector2) ToVector2()
        {
            return (true, Value);
        }

        public override bool SetVector2(Vector2 v)
        {
            Value = v;
            return true;
        }

        public override (bool, Vector3) ToVector3()
        {
            return (true, new Vector3(Value.X, Value.Y, 0));
        }

        public override bool SetVector3(Vector3 v)
        {
            Value = new Vector2(v.X, v.Y);
            return true;
        }

        public override void Reset()
        {
            Value = Vector2.Zero;
        }

        public override void Add(AValue x)
        {
            var v = x.ToVector2();
            if (v.Item1)
            {
                Value = Value + v.Item2;
            }
        }

        public override void Subtract(AValue x)
        {
            var v = x.ToVector2();
            if (v.Item1)
            {
                Value = Value - v.Item2;
            }
        }

        public override void Multiply(AValue x)
        {
            switch (x.ValueType)
            {
                case EValueType.Unknown: break;
                case EValueType.Boolean: break;

                case EValueType.Integer:
                case EValueType.Scalar:
                    var scalar = x.ToScalar();
                    if (scalar.Item1)
                    {
                        Value *= scalar.Item2;
                    }
                    break;

                case EValueType.Vector2:
                case EValueType.Vector3:
                    var vec2 = x.ToVector2();
                    if (vec2.Item1)
                    {
                        Value *= vec2.Item2;
                    }
                    break;

                case EValueType.String: break;
                case EValueType.Enum: break;
                default: break;
            }
        }

        public override void DivideBy(AValue x)
        {
            switch (x.ValueType)
            {
                case EValueType.Unknown: break;
                case EValueType.Boolean: break;

                case EValueType.Integer:
                case EValueType.Scalar:
                    var scalar = x.ToScalar();
                    if (scalar.Item1)
                    {
                        Value = Value / scalar.Item2;
                    }
                    break;

                case EValueType.Vector2:
                case EValueType.Vector3:
                    var vec2 = x.ToVector2();
                    if (vec2.Item1)
                    {
                        Value = Value / vec2.Item2;
                    }
                    break;

                case EValueType.String: break;
                case EValueType.Enum: break;
                default: break;
            }
        }

        public override void Negative()
        {
            Value = -Value;
        }

        /// <summary>
        /// Vector2必须严格地和同类型进行相等比较
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public override bool EqualsValue(AValue x)
        {
            if (x.ValueType == EValueType.Vector2)
            {
                var y = x as Value_Vector2;
                return Value == y.Value;
            }
            return false;
        }

        public override string ToString()
        {
            return Value.ToString();
        }

    }
}
